/**
 * Piece.
 * 
 * @author Brian Boadi, Antoine Domenger
 * @version 1.0
 */

package lp.chess;

public abstract class Piece {
	//- ATTRIBUTE(S) --------------------------------------------------------------------------

	/**
	 * The x coordinate of the piece.
	 */
	protected int m_x;
	
	/**
	 * The y coordinate of the piece.
	 */
	protected int m_y;
	
	/** 
	 * True if the piece is white, false if it is black.
	 */
	protected boolean m_white;
	
	//-----------------------------------------------------------------------------------------

	//- CONSTRUCTOR(S) ------------------------------------------------------------------------

	/**
 	 * Default constructor.
 	 * Initializes an empty piece.
 	 */
	public Piece() {
		init(-1, -1, true);
	}
	
	/**
	 * Initializes a new piece of a given color.
	 * 
	 * @param white true if the piece must be white, false if it must be black.
	 */
	public Piece(boolean white) {
		init(-1, -1, white);
	}
	
	/**
	 * Initializes a new piece with all of its attributes.
	 * 
	 * @param x	the x coordinate of the piece on the chessboard.
	 * @param y the y coordinate of the piece on the chessboard.
	 * @param white true if the piece must be white, false if it must be black.
	 */
	public Piece(int x, int y, boolean white) {
		init(x, y, white);
	}
	
	/**
	 * Copy constructor.
	 * Initializes a new piece from a given one.
	 * 
	 * @param other the piece to copy in the new one.
	 */
	public Piece(Piece other) {
		init(other.getX(), other.getY(), other.isWhite());
	}
	
	//-----------------------------------------------------------------------------------------
	
	//- GETTER(S) AND SETTER(S) ---------------------------------------------------------------
	
	/**
	 * Returns the x coordinate of the piece.
	 * 
	 * @return an integer representing the x coordinate of the piece.
	 */
	public int getX() {
		return m_x;
	}
	
	/**
	 * Returns the y coordinate of the piece.
	 * 
	 * @return an integer representing the y coordinate of the piece.
	 */
	public int getY() {
		return m_y;
	}
	
	/**
	 * Returns the color of the piece.
	 * 
	 * @return true if the piece is white.
	 */
	public boolean isWhite() {
		return m_white;
	}
	
	/**
	 * Returns the color of the piece.
	 * 
	 * @return true if the piece is black.
	 */
	public boolean isBlack() {
		return !isWhite();
	}
	
	/**
	 * Sets the x coordinate of the piece to the given one.
	 * 
	 * @param x the x coordinate of the piece.
	 */
	public void setX(int x) {
		m_x = x;
	}
	
	/**
	 * Sets the y coordinate of the piece to the given one.
	 * 
	 * @param y the y coordinate of the piece.
	 */
	public void setY(int y) {
		m_y = y;
	}
	
	/**
	 * Sets both the x and y coordinates of the piece to the given ones.
	 * 
	 * @param x the x coordinate of the piece.
	 * @param y the y coordinate of the piece.
	 */
	public void setPos(int x, int y) {
		setX(x);
		setY(y);
	}
	
	/**
	 * Sets the color of the piece.
	 * 
	 * @param white true if the piece must be white.
	 */
	public void setIsWhite(boolean white) {
		m_white = white;
	}
	
	/**
	 * Sets all the attributes of the piece.
	 * 
	 * @param x the x coordinate of the piece.
	 * @param y the y coordinate of the piece.
	 * @param white true if the piece must be white.
	 */
	public void init(int x, int y, boolean white) {
		setPos(x, y);
		setIsWhite(white);
	}
	
	//-----------------------------------------------------------------------------------------

	//- OTHER METHOD(S) -----------------------------------------------------------------------

	/**
	 * Moves the piece to the given position.
	 * 
	 * @param x the x coordinate where to move the piece.
	 * @param y the y coordinate where to move the piece.
	 */
	public void move(int x, int y) {
		setX(x);
		setY(y);
	}
	
	public Piece plusForte(Piece other) {
		if(getX()>other.getX() || getY()>other.getY()) return this;
		else return other;
	}
	
	/**
	 * Checks if a move is valid for this kind of piece.
	 * 
	 * @param cb the chessboard.
	 * @param x	the x coordinate where we want the piece to move on the chessboard.
	 * @param y the y coordinate where we want the piece to move on the chessboard.
	 * @param act true if we want to remove the piece that can be taken.
	 * 
	 * @return true if the move is valid.
	 */
	public boolean validMove(Chessboard cb, int x, int y, boolean act)
	{
		boolean isValid = true;
		
		if((getX() == cb.getPiece(x, y).getX()) && (getY() == cb.getPiece(x, y).getY()))
		{
			isValid = false;
		}
		
		return isValid;
	}
	
	/**
	 * Checks if a given position is different from the current position of this piece.
	 * 
	 * @param x	the x coordinate we want to check.
	 * @param y the y coordinate we want to check.
	 * 
	 * @return true if the position is different.
	 */
	public boolean differentPosition(int x, int y) {
		boolean isDifferent = true;
		
		if(x == getX() && y == getY())
		{
			System.out.println("This is the same position...");
			isDifferent = false;
		}
		
		return isDifferent;
	}
	
	/**
	 * Returns the character associated to the type of this piece.
	 * 
	 * @return the character associated to the type of this piece 
	 */
	public char getType()
	{
		if(isWhite()) return 'W';
		else return 'B';
	}
	
	/**
	 * Returns a copy of this piece.
	 * 
	 * @return a copy of this piece.
	 */
	public abstract Piece getCopy();
	
	//-----------------------------------------------------------------------------------------

	// DISPLAY METHOD(S) ----------------------------------------------------------------------

	/**
	 * Displays the piece in the console with all its attributes.
	 */
	void display()
	{
		System.out.println(getType() + "(" + getX() + ", " + getY() + ") " + (m_white ? " (white)" : " (black)") + "\n");
	}
	
	//-----------------------------------------------------------------------------------------	
}
